package bfs;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {

    public static void main(String[] args) {
        DefaultMutableTreeNode root = new DefaultMutableTreeNode(1);
        DefaultMutableTreeNode c11 = new DefaultMutableTreeNode(2);
        DefaultMutableTreeNode c12 = new DefaultMutableTreeNode(3);
        DefaultMutableTreeNode c112 = new DefaultMutableTreeNode(5);
        root.add(c11);
        root.add(c12);
        c11.add(c112);
        levelOrder(root);
    }

    public static List<List<Integer>> levelOrder(TreeNode root) {
        Queue<TreeNode> queue = new ConcurrentLinkedQueue();
        List<List<Integer>> resultList = new ArrayList();
        List levelList = new ArrayList();
        levelList.add(root);
        resultList.add(levelList);
        queue.add(root);
        while (!queue.isEmpty()){
            TreeNode firstNode = queue.poll();
            //业务逻辑
            System.out.println(firstNode);

            Enumeration<TreeNode> clist = firstNode.children();
            while(clist.hasMoreElements()){
                TreeNode node = clist.nextElement();
                queue.add(node);
            }
        }


        return resultList;
    }
}